package com.kgc.ssm.controller;

import com.kgc.ssm.entity.Material;
import com.kgc.ssm.entity.Orders;
import com.kgc.ssm.entity.dto.OrderDTO;
import com.kgc.ssm.entity.dto.Result;
import com.kgc.ssm.service.impl.OrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
public class OrderController {
    @Autowired
    private OrderServiceImpl orderService;

    @GetMapping("/findMaterial")
    public Result findMaterial(@RequestParam("materialNumPrefix") String materialNumPrefix,
                               @RequestParam("pageNum") Integer pageNum,
                               @RequestParam("pageSize") Integer pageSize) {
        System.out.println(materialNumPrefix + pageNum + pageSize);

        // 检查空值并设定默认值
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }

        List<Material> materials = orderService.findMaterial(materialNumPrefix, pageNum, pageSize);
        long countMaterial = orderService.countMaterial(materialNumPrefix);
        return new Result<>("物资已发送", 200, materials, countMaterial);
    }

    @GetMapping("/findMaterialByIds")
    public Result findMaterialByIds(String materialIds, Integer pageNum, Integer pageSize) {
        System.out.println(materialIds);
        // 检查空值并设定默认值
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        String[] split = materialIds.split(",");
        List<String> list = Arrays.asList(split);
        List<Material> materials = orderService.findMaterialByIds(list, pageNum, pageSize);
        long countMaterial = orderService.countFindMaterialByIds(list);
        return new Result<>("物资已发送", 200, materials, countMaterial);
    }

    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody Orders order) {
        int i = orderService.addOrder(order);
        return new Result<>("需求已保存", 200, null, null);
    }

    @GetMapping("/findOrders")
    public Result findOrders(@RequestParam(required = false) String materialCode,
                             @RequestParam(required = false) String materialName,
                             @RequestParam(required = false) String status,
                             @RequestParam(defaultValue = "1") int pageNum,
                             @RequestParam(defaultValue = "5") int pageSize) {
        Result<List<OrderDTO>> orders = orderService.getOrders(materialCode, materialName, status, pageNum, pageSize);
        return orders;
    }

    @PostMapping("/updateOrderStatus")
    public Result updateOrderStatus(@RequestParam(required = false)String orderIds) {
        String[] split = orderIds.split(",");
        Integer[] integers = Arrays.stream(split)
                .map(Integer::valueOf)
                .toArray(Integer[]::new);
        List<Integer> list = Arrays.asList(integers);
        orderService.updateOrderStatus(list,"C001-20");
        return new Result<>("状态已更新", 200, null, null);
    }
}

